package com.yzg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yzg.exceptionhandler.ServiceException;
import com.yzg.mapper.ProductAttributeCategoryMapper;
import com.yzg.mapper.ProductAttributeValueMapper;
import com.yzg.mapper.ProductCategoryAttributeRelationMapper;
import com.yzg.pojo.ProductAttributeCategory;
import com.yzg.pojo.ProductAttributeValue;
import com.yzg.pojo.ProductCategoryAttributeRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzg.pojo.ProductAttribute;
import com.yzg.mapper.ProductAttributeMapper;
import com.yzg.service.ProductAttributeService;
/**
* @Author: decade
* @Description: ${description}
* @Date: 2021/1/12 13:10
**/
@Service
public class ProductAttributeServiceImpl implements ProductAttributeService{
    @Autowired
    private ProductAttributeMapper productAttributeMapper;
    @Autowired
    private ProductAttributeValueMapper productAttributeValueMapper;
    @Autowired
    private ProductCategoryAttributeRelationMapper productCategoryAttributeRelationMapper;
    @Autowired
    private ProductAttributeCategoryMapper productAttributeCategoryMapper;

    //根据商品属性分类id获取所有商品属性列表
    @Override
    public List<ProductAttribute> getAttrListById(IPage<ProductAttribute> pageParam,Long id,Integer type) {
//        System.out.println(type);
//        if(type!=0||type!=1){
//            throw new ServiceException();
//        }
        QueryWrapper<ProductAttribute> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        wrapper.eq("product_attribute_category_id",id);
        productAttributeMapper.selectPage(pageParam,wrapper);
        return pageParam.getRecords();
    }

    //商品属性编辑
    @Override
    public int updateProductAttributeById(ProductAttribute productAttribute) {
        return productAttributeMapper.updateById(productAttribute);
    }

    //根据商品属性id获取商品属性
    @Override
    public ProductAttribute getProductAttrById(Long id) {
        return productAttributeMapper.selectById(id);
    }

    //商品属性添加
    @Override
    public int addProductAttr(ProductAttribute productAttr) {
        //更新商品属性分类的属性数量
        //根据父id查询商品属性分类
        ProductAttributeCategory productAttrCate =
                productAttributeCategoryMapper.selectById(productAttr.getProductAttributeCategoryId());
        if (productAttr.getType()==0){
            productAttrCate.setAttributeCount(productAttrCate.getAttributeCount()+1);
        }else{
            productAttrCate.setParamCount(productAttrCate.getParamCount()+1);
        }
        productAttributeCategoryMapper.updateById(productAttrCate);
        //最后添加
        return productAttributeMapper.insert(productAttr);
    }

    //商品属性删除
    @Override
    public int deleteProductAttrById(Long id) {
        //删除与之关联的商品属性值表，
        UpdateWrapper<ProductAttributeValue> wrapper = new UpdateWrapper<>();
        wrapper.eq("product_attribute_id ",id);
        productAttributeValueMapper.delete(wrapper);
        //删除商品分类和属性的关系表
        UpdateWrapper<ProductCategoryAttributeRelation> wrapper1 = new UpdateWrapper<>();
        wrapper1.eq("product_attribute_id",id);
        productCategoryAttributeRelationMapper.delete(wrapper1);
        //然后更新商品属性分类的属性数量
        //根据id查询商品属性
        ProductAttribute productAttr = this.getProductAttrById(id);
//        ProductAttributeCategory productAttributeCategory = new ProductAttributeCategory();
        //根据父id查询商品属性分类
        ProductAttributeCategory productAttrCate =
            productAttributeCategoryMapper.selectById(productAttr.getProductAttributeCategoryId());

        if (productAttr.getType()==0){
            productAttrCate.setAttributeCount(productAttrCate.getAttributeCount()-1);
        }else{
            productAttrCate.setParamCount(productAttrCate.getParamCount()-1);
        }
        productAttributeCategoryMapper.updateById(productAttrCate);
        //最后删除
        return productAttributeMapper.deleteById(id);
    }
}
